﻿using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using NHibernate;
using Landscape.Business.FNH.Activities;
using Landscape.Business.FNH;
using Landscape.Business;
using System.Web;

namespace Landscape.WebApp.WCFServices
{
    [ServiceContract(Namespace = "Landscape")]
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class OperationsActions
    {
        #region Actiontrack Methods, Not required at this momment        

        //[OperationContract]
        //public Guid SaveAction(Action pBizObject)
        //{
        //    Guid CARID = Guid.Empty;
        //    using (var nhSession = NHSessionManager.GetManager().GetSession())
        //    {
        //        using (var trans = nhSession.BeginTransaction())
        //        {
        //            OperationsActivities oa = new OperationsActivities(nhSession);
        //            CARID = oa.SaveAction(pBizObject.ToBusinessObject(nhSession));

        //            pBizObject.ID = CARID;

        //            trans.Commit();
        //        }
        //    }

        //    if (pBizObject.IsReleased)
        //    {
        //        ActionTrackWFProxy.StartActionTrackClient wfStart = new ActionTrackWFProxy.StartActionTrackClient();
        //        wfStart.StartActionTrack(pBizObject.ID, new Guid[] { pBizObject.AssignedTo }, pBizObject.ResponseDueDate.Value, pBizObject.Priority);
        //    }

        //    return CARID;
        //}

        //[OperationContract]
        //public List<UserMaster> GetReviewers(string pActionSource, Guid? pVesselCode, Guid? pDepartmentCode)
        //{
        //    var actionSource = (Business.ActionSource)Enum.Parse(typeof(Business.ActionSource), pActionSource);
        //    string roleNames = string.Empty;
        //    List<UserMaster> users = new List<UserMaster>();
        //    AdminActivities aa = new AdminActivities();

        //    if (actionSource == ActionSource.OEA ||
        //        actionSource == ActionSource.OIA ||
        //        actionSource == ActionSource.MR ||
        //        actionSource == ActionSource.SI)
        //    {
        //        pVesselCode = null;
        //    }
            

        //    if (actionSource == ActionSource.OEA ||
        //        actionSource == ActionSource.OIA)
        //    {
        //        pDepartmentCode = null;
        //    }
                              

        //    #region Select the roles based on source of the action item.

        //    switch (actionSource)
        //    {
        //        case ActionSource.OEA:
        //            {
        //                roleNames = "Manager";
        //                pDepartmentCode = aa.GetDepartmentByName("HSEQ").ID;
        //                break;
        //            }
        //        case ActionSource.CFB:
        //            {
        //                roleNames = "Manager";
        //                pDepartmentCode = aa.GetDepartmentByName("HSEQ").ID;
        //                break;
        //            }
        //        case ActionSource.OIA:
        //            {
        //                roleNames = "Manager";
        //                break;
        //            }
        //        case ActionSource.MR:
        //            {
        //                roleNames = "Manager";
        //                break;
        //            }
        //        case ActionSource.INC:
        //            {
        //                roleNames = "Manager";
        //                pDepartmentCode = aa.GetDepartmentByName("HSEQ").ID;
        //                break;
        //            }
        //        case ActionSource.NC:
        //            {
        //                roleNames = "Manager";
        //                break;
        //            }
        //        case ActionSource.SCM:
        //            {
        //                roleNames = "Manager";
        //                break;
        //            }
        //        case ActionSource.EM:
        //            {
        //                roleNames = "Manager";
        //                break;
        //            }
        //        case ActionSource.SA:
        //            {
        //                roleNames = "Manager";
        //                break;
        //            }
        //        case ActionSource.SI:
        //            {
        //                roleNames = "Manager";
        //                break;
        //            }
        //        case ActionSource.PI:
        //            {
        //                roleNames = "Manager";
        //                break;
        //            }
        //        case ActionSource.MM:
        //            {
        //                roleNames = "Manager";
        //                break;
        //            }
        //        default:
        //            break;
        //    }
        //    #endregion

            
        //    //The following code was commented after discussion with Ravi.
        //    //The commented code will return the users who are either vessel department manager or department managers based on the selected source.
        //    //After discussion, reviewr can be any manager irrespective of department and/or vessel.            

        //    foreach (var role in roleNames.Split(','))
        //    {
        //        var r = aa.GetRoleByName(role);
        //        //if (r != null)
        //          //  r.Resources                        
        //            //    .ToList().ForEach(resmap => { if (users.Count((usr) => { return usr.ID == resmap.User.ID; }) == 0)users.Add(new UserMaster(resmap.User)); });
        //    }

        //    return users;
        //}

        //[OperationContract]
        //public Action GetAction(Guid pActionID)
        //{
        //    using (var nhSession = NHSessionManager.GetManager().GetSession())
        //    {
        //        OperationsActivities oa = new OperationsActivities(nhSession);
        //        return new Action(oa.GetCorrectiveActionByID(pActionID));
        //    }
        //}

        //[OperationContract]
        //public ActionResponse GetActiveResponse(Guid pActionID)
        //{
        //    using (var nhSession = NHSessionManager.GetManager().GetSession())
        //    {
        //        OperationsActivities oa = new OperationsActivities(nhSession);
        //        var action = oa.GetCorrectiveActionByID(pActionID);

        //        if (action.Responses.OrderByDescending(exp => exp.ResponseIndex).Count() > 0)
        //        {
        //            return new ActionResponse(action.Responses.OrderByDescending(exp => exp.ResponseIndex).Take(1).SingleOrDefault());
        //        }
        //        else
        //        {
        //            return null;
        //        }
        //    }
        //}

        //[OperationContract]
        //public Guid SaveResponse(Guid pActionID, ActionResponse pResponseObject, string pAction)
        //{
        //     Guid CARID = Guid.Empty;
        //    using (var nhSession = NHSessionManager.GetManager().GetSession())
        //    {
        //        using (var trans = nhSession.BeginTransaction())
        //        {
        //            OperationsActivities oa = new OperationsActivities(nhSession);
        //            oa.SaveActionResponse(pActionID, pResponseObject.ToBusinessObject(nhSession), pAction);
        //            trans.Commit();

        //            var action = oa.GetCorrectiveActionByID(pActionID);
        //            CARID = action.Responses.Where(exp => exp.Response == pResponseObject.Response).OrderByDescending(exp=>exp.UpdatedDate).Take(1).Single().ID;
        //        }
        //    }

        //    return CARID;
        //}

        //[OperationContract]
        //public void ReleaseResponse(Guid pActionID, ActionResponse pResponseObject, string pAction)
        //{
        //    switch (pAction.ToLower())
        //    {
        //        case "respond":
        //            {
        //                pResponseObject.Status = "RELEASED";
        //                pResponseObject.IsResponseReleased = true;
        //                //pResponseObject.ResponseDate = DateTime.Now;
        //                this.SaveResponse(pActionID, pResponseObject, pAction);

        //                ActionTrackWFProxy.RespondClient wfRespond = new ActionTrackWFProxy.RespondClient();
        //                wfRespond.Respond(pActionID, pResponseObject.Response);
        //                break;
        //            }
        //        case "review":
        //            {
        //                pResponseObject.IsReviewReleased = true;
        //                this.SaveResponse(pActionID, pResponseObject, pAction);

        //                ActionTrackWFProxy.ReviewDoneClient wfReview = new ActionTrackWFProxy.ReviewDoneClient();
        //                wfReview.ReviewDone(pResponseObject.ReviewerComments, pActionID, pResponseObject.ReviewStatus);
        //                break;
        //            }
        //        case "rework":
        //            {
        //                pResponseObject.Status = "RELEASED";
        //                pResponseObject.IsResponseReleased = true;
        //                this.SaveResponse(pActionID, pResponseObject, pAction);

        //                ActionTrackWFProxy.ReworkResponseClient wfRespond = new ActionTrackWFProxy.ReworkResponseClient();
        //                wfRespond.ReworkResponse(pActionID, pResponseObject.Response);

        //                break;
        //            }
        //        case "hseqapproval":
        //            {
        //                pResponseObject.IsHseqApprovalReleased = true;
        //                this.SaveResponse(pActionID, pResponseObject, pAction);

        //                ActionTrackWFProxy.HSEQApprovalClient wfReview = new ActionTrackWFProxy.HSEQApprovalClient();
        //                wfReview.HSEQApproval(pActionID, pResponseObject.HseqComments, pResponseObject.HseqApprovalStatus);
        //                break;
        //            }
        //        case "odapproval":
        //            {
        //                pResponseObject.IsOdApprovalReleased = true;
        //                this.SaveResponse(pActionID, pResponseObject, pAction);

        //                ActionTrackWFProxy.OpsDirApprovalClient wfReview = new ActionTrackWFProxy.OpsDirApprovalClient();
        //                wfReview.OpsDirApproval(pActionID, pResponseObject.OdComments, pResponseObject.OdApprovalStatus);
        //                break;
        //            }
        //    }
        //}

        //[OperationContract]
        //public List<SearchActionResult> SearchActions(List<object> pSearchParams, int pPageNo, int pPageCount)
        //{
        //    List<SearchActionResult> result = new List<SearchActionResult>();

        //    using (var nhSession = NHSessionManager.GetManager().GetSession())
        //    {
        //        OperationsActivities oa = new OperationsActivities(nhSession);
        //        var allActions = oa.SearchActions(pSearchParams, pPageNo, pPageCount);

        //        foreach(var act in allActions)
        //        {
        //            string actType = (act.ActionType != null ? (act.ActionType == ActionType.IMPROVEMENT ? "Improvement Action" : "Corrective Action") : "-");
        //            var source = string.Empty;

        //            #region Set the source type
                    
        //            if (act.Source != null)
        //            {
        //                switch (act.Source)
        //                {
        //                    case ActionSource.OEA:
        //                        source = "Office external audit";
        //                        break;
        //                    case ActionSource.CFB:
        //                        source = "Client feedback";
        //                        break;
        //                    case ActionSource.OIA:
        //                        source = "Office internal audit";
        //                        break;
        //                    case ActionSource.MR:
        //                        source = "Management review";
        //                        break;
        //                    case ActionSource.INC:
        //                        source = "Office input for incident on vessel";
        //                        break;
        //                    case ActionSource.NC:
        //                        source = "Office input for NC raised by vessel";
        //                        break;
        //                    case ActionSource.SCM:
        //                        source = "Office input for issue raised through SCM review";
        //                        break;
        //                    case ActionSource.EM:
        //                        source = "Emergency management feedback";
        //                        break;
        //                    case ActionSource.SA:
        //                        source = "Supply alert";
        //                        break;
        //                    case ActionSource.SI:
        //                        source = "System improvement";
        //                        break;
        //                    case ActionSource.PI:
        //                        source = "Project interface";
        //                        break;
        //                    case ActionSource.MM:
        //                        source = "Morning meeting";
        //                        break;
        //                    default:
        //                        source = "-";
        //                        break;
        //                }
        //            }
        //            else
        //            {
        //                source = "-";
        //            }

        //            #endregion

        //            var status = string.Empty;

        //            #region Set status type

        //            switch (act.Status)
        //            {
        //                case ActionStatus.UNDEFINED:
        //                    status = "-";
        //                    break;
        //                case ActionStatus.NEW:
        //                    status = "Draft";
        //                    break;
        //                case ActionStatus.RELEASED:
        //                    status = "Finalized";
        //                    break;
        //                case ActionStatus.INPROGRESS:
        //                    status = "For Response";
        //                    break;
        //                case ActionStatus.WAIT_REVIEW:
        //                    status = "For Review";
        //                    break;
        //                case ActionStatus.REWORK:
        //                    status = "For Rework";
        //                    break;
        //                case ActionStatus.WAIT_HSEQ:
        //                    status = "For HSEQ Aproval";
        //                    break;
        //                case ActionStatus.WAIT_OD:
        //                    status = "For OD Approval";
        //                    break;
        //                case ActionStatus.CLOSED:
        //                    status = "Closed";
        //                    break;
        //                default:
        //                    break;
        //            }

        //            #endregion

        //            AdminActivities aa = new AdminActivities(nhSession);
        //            var createdUser = aa.GetUserMasterByID(act.CreatedBy);

        //            var actObject = new SearchActionResult()
        //            {
        //                ID = act.ID,
        //                Code = act.Code,
        //                Title = act.Title,
        //                ActType = actType,
        //                Source = source,
        //                AssignedTo = (act.Responder != null ? act.Responder.FirstName + " " + act.Responder.LastName : "-"),
        //                ExpectedDate = (act.ResponseDueDate.HasValue ? act.ResponseDueDate.Value.ToString("dd-MMM-yyyy") : "-"),
        //                Status = status,
        //                CreatedBy = createdUser.FirstName + " " + createdUser.LastName,
        //                CreatedDate = act.CreatedDate.ToString("dd-MMM-yyyy"),
        //                FinalizeDate = (act.ReleasedDate.HasValue ? act.ReleasedDate.Value.ToString("dd-MMM-yyyy") : "-"),
        //                Priority = (act.Priority == ActionPriority.HIGH ? "High" : (act.Priority == ActionPriority.LOW ? "Low" : "Normal")),
        //                ClosedDate = (act.Status == ActionStatus.CLOSED ? act.UpdatedDate.ToString("dd-MMM-yyyy") : "-")
        //            };

        //            result.Add(actObject);
        //        }
        //    }

        //    return result;
        //}

        //[OperationContract]
        //public int SearchActionsCount(List<object> pSearchParams)
        //{
        //    int cnt = 0;

        //    using (var nhSession = NHSessionManager.GetManager().GetSession())
        //    {
        //        OperationsActivities oa = new OperationsActivities(nhSession);
        //        var allActions = oa.SearchActions(pSearchParams, -1, 0);

        //        if (allActions != null)
        //            cnt = allActions.Count;
        //    }

        //    return cnt;
        //}

        //[OperationContract]
        //public void CommitActionFiles(List<Attachment> pFilesList, Guid pIncID)
        //{
        //    using (var nhSession = NHSessionManager.GetManager().GetSession())
        //    {
        //        using (var trans = nhSession.BeginTransaction())
        //        {
        //            foreach (Attachment item in pFilesList)
        //            {
        //                OperationsActivities oa = new OperationsActivities(nhSession);
        //                var filePath = HttpContext.Current.Server.MapPath("~/TempFiles") + "\\" + item.FileName;
        //                oa.SaveActionFile(pIncID, filePath, item.FileName, string.Empty, item.FileSize, item.UploadedBy);
        //            }

        //            trans.Commit();
        //        }
        //    }
        //}

        //[OperationContract]
        //public List<Attachment> GetFilesForAction(Guid pActID, int pPageNo, int pPageSize)
        //{
        //    List<Attachment> fileAttachments = new List<Attachment>();
        //    using (var nhSession = NHSessionManager.GetManager().GetSession())
        //    {
        //        OperationsActivities ea = new OperationsActivities(nhSession);
        //        var inc = ea.GetCorrectiveActionByID(pActID);
        //        foreach (var att in (pPageSize != -1 ? inc.Attachments.Skip(pPageNo * pPageSize).Take(pPageSize) : inc.Attachments))
        //        {
        //            fileAttachments.Add(new Attachment(att));
        //        }
        //    }

        //    return fileAttachments;
        //}

        //[OperationContract]
        //public int GetActionFileCount(Guid pActID)
        //{
        //    using (var nhSession = NHSessionManager.GetManager().GetSession())
        //    {
        //        OperationsActivities ea = new OperationsActivities(nhSession);
        //        var inc = ea.GetCorrectiveActionByID(pActID);
        //        return inc.Attachments.Count;
        //    }
        //}

        //[OperationContract]
        //public List<Attachment> GetFilesForActionResponse(Guid pActID, Guid pActRespID, int pPageNo, int pPageSize)
        //{
        //    List<Attachment> fileAttachments = new List<Attachment>();
        //    using (var nhSession = NHSessionManager.GetManager().GetSession())
        //    {
        //        OperationsActivities ea = new OperationsActivities(nhSession);
        //        var inc = ea.GetCorrectiveActionByID(pActID);
        //        var resp = inc.Responses.Where(exp => exp.ID == pActRespID).Single();
        //        foreach (var att in (pPageSize != -1 ? resp.Attachments.Skip(pPageNo * pPageSize).Take(pPageSize) : resp.Attachments))
        //        {
        //            fileAttachments.Add(new Attachment(att));
        //        }
        //    }

        //    return fileAttachments;
        //}

        //[OperationContract]
        //public int GetActionResponseFileCount(Guid pActID, Guid pActRespID)
        //{
        //    using (var nhSession = NHSessionManager.GetManager().GetSession())
        //    {
        //        OperationsActivities ea = new OperationsActivities(nhSession);
        //        var inc = ea.GetCorrectiveActionByID(pActID);
        //        var resp = inc.Responses.Where(exp => exp.ID == pActRespID).Single();
        //        return resp.Attachments.Count;
        //    }
        //}

        //[OperationContract]
        //public void CommitActionResponseFiles(List<Attachment> pFilesList, Guid pActID, Guid pRespID)
        //{
        //    using (var nhSession = NHSessionManager.GetManager().GetSession())
        //    {
        //        using (var trans = nhSession.BeginTransaction())
        //        {
        //            foreach (Attachment item in pFilesList)
        //            {
        //                OperationsActivities oa = new OperationsActivities(nhSession);
        //                var filePath = HttpContext.Current.Server.MapPath("~/TempFiles") + "\\" + item.FileName;
        //                oa.CommitActionResponseFiles(pActID, pRespID, filePath, item.FileName, string.Empty, item.FileSize, item.UploadedBy);
        //            }

        //            trans.Commit();
        //        }
        //    }
        //}

        //[OperationContract]
        //public bool HasResponseHistory(Guid pActID)
        //{
        //    using (var nhSession = NHSessionManager.GetManager().GetSession())
        //    {
        //        OperationsActivities ea = new OperationsActivities(nhSession);
        //        var inc = ea.GetCorrectiveActionByID(pActID);

        //        return inc.Responses.Where(exp => exp.IsResponseReleased).Count() > 1;
        //    }
        //}

        #endregion
    }

    [DataContract(Namespace = "Landscape")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Action
    {
        public Action()
        {
            this.ID = Guid.Empty;
        }

        public Action(Landscape.Business.Entities.Action pBusinessObject)
        {
            this.ID = pBusinessObject.ID;
            this.Code = pBusinessObject.Code;
            this.Title = pBusinessObject.Title;
            this.Description = pBusinessObject.Description;
            this.AssignedTo = (pBusinessObject.Responder != null ? pBusinessObject.Responder.ID : Guid.Empty);
            this.Reviewers = pBusinessObject.Reviewers.Select(Exp=>Exp.ID).ToList();

            if (pBusinessObject.ResponseDueDate.HasValue)
                this.ResponseDueDate = DateTime.SpecifyKind(pBusinessObject.ResponseDueDate.Value, DateTimeKind.Local);

            this.Priority = pBusinessObject.Priority.ToString();
            this.Type = pBusinessObject.ActionType.ToString();
            this.Source = pBusinessObject.Source.ToString();
            this.Status = pBusinessObject.Status.ToString();            
            this.CreatedBy = pBusinessObject.CreatedBy;
            this.CreatedDate = pBusinessObject.CreatedDate;
            this.UpdatedBy = pBusinessObject.UpdatedBy;
            this.UpdatedDate = pBusinessObject.UpdatedDate;
            this.NonConforDescription = pBusinessObject.NonConforDescription;
            this.VesselCode = (pBusinessObject.Vessel != null ? pBusinessObject.Vessel.ID : Guid.Empty);
            this.DepartmentCode = (pBusinessObject.Department != null ? pBusinessObject.Department.ID : Guid.Empty);
            this.IsReleased = pBusinessObject.IsReleased;
            this.ReleasedDate = pBusinessObject.ReleasedDate;
            this.ReleasedBy = pBusinessObject.ReleasedBy;            

            AdministrativeActions aActs = new AdministrativeActions();
            UserMaster um = null;

            if (this.AssignedTo != Guid.Empty)
            {
                um = aActs.GetUserByID(this.AssignedTo);
                this.AssignedUser = um.FirstName + " " + um.LastName;
            }

            if (this.ReleasedBy != Guid.Empty)
            {
                um = aActs.GetUserByID(this.ReleasedBy);
                this.ReleasedUser = um.FirstName + " " + um.LastName;
            }

            if (this.Reviewers!= null && this.Reviewers.Count>0)
            {
                this.ReviewUsers = new Dictionary<Guid, string>();
                foreach (var reviewer in pBusinessObject.Reviewers)
                {
                    //um = aActs.GetUserByID(reviewer.ID);
                    this.ReviewUsers.Add(reviewer.ID, reviewer.FirstName + " " + reviewer.LastName);
                }
            }

            if (this.CreatedBy != Guid.Empty)
            {
                um = aActs.GetUserByID(this.CreatedBy);
                this.CreatedUser = um.FirstName + " " + um.LastName;
            }

            if (this.UpdatedBy != Guid.Empty)
            {
                um = aActs.GetUserByID(this.UpdatedBy);
                this.UpdatedUser = um.FirstName + " " + um.LastName;
            }

            if (pBusinessObject.ReleasedBy != Guid.Empty)
            {
                um = aActs.GetUserByID(pBusinessObject.ReleasedBy);
                this.ReleasedUser = um.FirstName + " " + um.LastName;
            }
        }

        [DataMember]
        public Guid ID
        { get; set; }

        [DataMember]
        public string Code
        { get; set; }

        [DataMember]
        public string Title
        { get; set; }

        [DataMember]
        public string Description
        { get; set; }

        [DataMember]
        public string NonConforDescription
        { get; set; }

        [DataMember]
        public Guid AssignedTo
        { get; set; }

        [DataMember]
        public string AssignedUser
        { get; set; }

        [DataMember]
        public List<Guid> Reviewers
        { get; set; }

        [DataMember]
        public Dictionary<Guid, string> ReviewUsers
        { get; set; }

        [DataMember]
        public DateTime? ResponseDueDate
        { get; set; }

        [DataMember]
        public string Priority
        { get; set; }

        [DataMember]
        public string Type
        { get; set; }

        [DataMember]
        public string Source
        { get; set; }

        [DataMember]
        public string Status
        { get; set; }

        [DataMember]
        public bool IsReleased
        { get; set; }

        [DataMember]
        public Guid ReleasedBy
        { get; set; }

        [DataMember]
        public string ReleasedUser
        { get; set; }

        [DataMember]
        public DateTime? ReleasedDate
        { get; set; }

        [DataMember]
        public Guid VesselCode
        { get; set; }

        [DataMember]
        public Guid DepartmentCode
        { get; set; }

        [DataMember]
        public Guid CreatedBy
        { get; set; }

        [DataMember]
        public string CreatedUser
        { get; set; }

        [DataMember]
        public DateTime CreatedDate
        { get; set; }

        [DataMember]
        public Guid UpdatedBy
        { get; set; }

        [DataMember]
        public string UpdatedUser
        { get; set; }

        [DataMember]
        public DateTime UpdatedDate
        { get; set; }

        public Landscape.Business.Entities.Action ToBusinessObject(ISession pSession)
        {
            Landscape.Business.Entities.Action bizObject = null;

            if (this.ID != Guid.Empty)
            {
                OperationsActivities oa = new OperationsActivities(pSession);

                bizObject = oa.GetCorrectiveActionByID(this.ID);
            }
            else
            {
                bizObject = new Business.Entities.Action();
                bizObject.CreatedBy = this.CreatedBy;
                bizObject.CreatedDate = DateTime.Now;
                bizObject.IsDeleted = false;
                bizObject.IsReleased = false;
            }

            bizObject.Title = this.Title;
            bizObject.Description = this.Description;

            if (this.AssignedTo != Guid.Empty)
                bizObject.Responder = new Business.Entities.UserMaster() { ID = this.AssignedTo };

            Business.ActionPriority priority;
            if (Enum.TryParse<Business.ActionPriority>(this.Priority, out priority))
                bizObject.Priority = priority;

            //bizObject.Priority = (Business.ActionPriority)Enum.Parse(typeof(Business.ActionPriority), this.Priority);

            if (this.ResponseDueDate.HasValue)
                bizObject.ResponseDueDate = this.ResponseDueDate.Value.ToLocalTime();
            else
                bizObject.ResponseDueDate = this.ResponseDueDate;

            if (this.Reviewers != null)
            {
                if (bizObject.Reviewers.Count > 0)
                {
                    while (bizObject.Reviewers.Count > 0)
                    {
                        bizObject.Reviewers.RemoveAt(0);
                    }
                }
                this.Reviewers.ForEach(itm => { if (itm != null && itm != Guid.Empty)bizObject.Reviewers.Add(new Business.Entities.UserMaster() { ID = itm }); });
            }

            Business.ActionSource source;
            if (Enum.TryParse<Business.ActionSource>(this.Source, out source))
                bizObject.Source = source;

            Business.ActionStatus status;
            if (Enum.TryParse<Business.ActionStatus>(this.Status, out status))
                bizObject.Status = status;

            Business.ActionType actionType;
            if (Enum.TryParse<Business.ActionType>(this.Type, out actionType))
                bizObject.ActionType = actionType;

            bizObject.NonConforDescription = this.NonConforDescription;
            bizObject.IsReleased = this.IsReleased;
            bizObject.ReleasedDate = this.ReleasedDate;
            bizObject.ReleasedBy = this.ReleasedBy;            

            AdminActivities aa = new AdminActivities(pSession);

            if (bizObject.Vessel == null || bizObject.Vessel.ID != this.VesselCode)
                bizObject.Vessel = aa.GetVesselByCode(this.VesselCode);

            if (bizObject.Department == null || bizObject.Department.ID != this.DepartmentCode)
                bizObject.Department = aa.GetDepartmentByID(this.DepartmentCode);

            return bizObject;
        }
    }

    [DataContract(Namespace = "Landscape")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ActionResponse
    {
        public ActionResponse()
        { this.ID = Guid.Empty; }

        public ActionResponse(Business.Entities.ActionResponse pBusinessObject)
        {
            this.ID = pBusinessObject.ID;
            this.Response = pBusinessObject.Response;
            this.ResponseBy = pBusinessObject.ResponseBy;
            this.ResponseDate = pBusinessObject.ResponseDate.ToString("dd-MMM-yyyy");
            this.ResponseIndex = pBusinessObject.ResponseIndex;
            this.AttachmentCount = (pBusinessObject.Attachments != null ? pBusinessObject.Attachments.Count() : 0);
            this.ReviewerComments = pBusinessObject.ReviewerComments;
            this.ReviewStatus = pBusinessObject.ReviewStatus;

            if (pBusinessObject.ReviewDate == DateTime.MinValue)
                this.ReviewDate = string.Empty;
            else
                this.ReviewDate = pBusinessObject.ReviewDate.ToString("dd-MMM-yyyy");

            this.HseqComments = pBusinessObject.HseqComments;
            this.HseqApprovalStatus = pBusinessObject.HseqApprovalStatus;

            if (pBusinessObject.HseqApprovalDate == DateTime.MinValue)
                this.HseqApprovalDate = string.Empty;
            else
                this.HseqApprovalDate = pBusinessObject.HseqApprovalDate.ToString("dd-MMM-yyyy");

            this.OdComments = pBusinessObject.OdComments;
            this.OdApprovalStatus = pBusinessObject.OdApprovalStatus;

            if (pBusinessObject.OdApprovalDate == DateTime.MinValue)
                this.OdApprovalDate = string.Empty;
            else
                this.OdApprovalDate = pBusinessObject.OdApprovalDate.ToString("dd-MMM-yyyy");

            this.CreatedBy = pBusinessObject.CreatedBy;
            this.CreatedDate = pBusinessObject.CreatedDate;
            this.UpdatedBy = pBusinessObject.UpdatedBy;
            this.UpdatedDate = pBusinessObject.UpdatedDate;
            this.Status = pBusinessObject.Status.ToString();

            this.IsResponseReleased = pBusinessObject.IsResponseReleased;
            this.IsReviewReleased = pBusinessObject.IsReviewReleased;
            this.IsHseqApprovalReleased = pBusinessObject.IsHseqApprovalReleased;
            this.IsOdApprovalReleased = pBusinessObject.IsOdApprovalReleased;

            this.ResponseDate = (pBusinessObject.ResponseDate != DateTime.MinValue ? pBusinessObject.ResponseDate.ToString("dd-MMM-yyyy") : "");
            this.ReviewDate = (pBusinessObject.ReviewDate != DateTime.MinValue ? pBusinessObject.ReviewDate.ToString("dd-MMM-yyyy") : "");
            this.HseqApprovalDate = (pBusinessObject.HseqApprovalDate != DateTime.MinValue ? pBusinessObject.HseqApprovalDate.ToString("dd-MMM-yyyy") : "");
            this.OdApprovalDate = (pBusinessObject.OdApprovalDate != DateTime.MinValue ? pBusinessObject.OdApprovalDate.ToString("dd-MMM-yyyy") : "");

            AdminActivities aa = new AdminActivities();

            if (this.ResponseBy != Guid.Empty)
            {
                var ud = aa.GetUserMasterByID(this.ResponseBy);
                if (ud != null)
                    this.ResponseUser = ud.FirstName + " " + ud.LastName;
            }

            this.HseqCommentsBy = pBusinessObject.HseqCommentsBy;
            if (this.HseqCommentsBy != Guid.Empty)
            {
                var ud = aa.GetUserMasterByID(this.HseqCommentsBy);
                if (ud != null)
                    this.HseqCommentsUser = ud.FirstName + " " + ud.LastName;
            }

            this.OdCommentsBy = pBusinessObject.OdCommentsBy;
            if (this.OdCommentsBy != Guid.Empty)
            {
                var ud = aa.GetUserMasterByID(this.OdCommentsBy);
                if (ud != null)
                    this.OdCommentsUser = ud.FirstName + " " + ud.LastName;
            }

            this.ReviewCommentsBy = pBusinessObject.ReviewCommentsBy;
            if (this.ReviewCommentsBy != Guid.Empty)
            {
                var ud = aa.GetUserMasterByID(this.ReviewCommentsBy);
                if (ud != null)
                    this.ReviewCommentsUser = ud.FirstName + " " + ud.LastName;
            }
        }

        [DataMember]
        public Guid ID
        { get; set; }

        [DataMember]
        public string Response
        { get; set; }

        [DataMember]
        public Guid ResponseBy
        { get; set; }

        [DataMember]
        public string ResponseUser
        { get; set; }

        [DataMember]
        public string ResponseDate
        { get; set; }

        [DataMember]
        public int ResponseIndex
        { get; set; }

        [DataMember]
        public bool IsResponseReleased
        { get; set; }
                
        [DataMember]
        public int AttachmentCount
        { get; set; }

        [DataMember]
        public string Status
        { get; set; }

        [DataMember]
        public Guid ReviewCommentsBy
        { get; set; }

        [DataMember]
        public string ReviewCommentsUser
        { get; set; }

        [DataMember]
        public string ReviewerComments
        { get; set; }

        [DataMember]
        public string ReviewStatus
        { get; set; }

        [DataMember]
        public string ReviewDate
        { get; set; }

        [DataMember]
        public bool IsReviewReleased
        { get; set; }

        [DataMember]
        public string HseqCommentsUser
        { get; set; }

        [DataMember]
        public Guid HseqCommentsBy
        { get; set; }

        [DataMember]
        public string HseqComments
        { get; set; }

        [DataMember]
        public string HseqApprovalStatus
        { get; set; }

        [DataMember]
        public string HseqApprovalDate
        { get; set; }

        [DataMember]
        public bool IsHseqApprovalReleased
        { get; set; }

        [DataMember]
        public string OdCommentsUser
        { get; set; }

        [DataMember]
        public Guid OdCommentsBy
        { get; set; }

        [DataMember]
        public string OdComments
        { get; set; }

        [DataMember]
        public string OdApprovalStatus
        { get; set; }

        [DataMember]
        public string OdApprovalDate
        { get; set; }

        [DataMember]
        public bool IsOdApprovalReleased
        { get; set; }

        [DataMember]
        public Guid CreatedBy
        { get; set; }

        [DataMember]
        public DateTime CreatedDate
        { get; set; }

        [DataMember]
        public Guid UpdatedBy
        { get; set; }

        [DataMember]
        public DateTime UpdatedDate
        { get; set; }

        public Landscape.Business.Entities.ActionResponse ToBusinessObject(ISession pSession)
        {
            Landscape.Business.Entities.ActionResponse bizObject = null;

            if (this.ID != Guid.Empty)
            {
                OperationsActivities oa = new OperationsActivities(pSession);

                //bizObject = oa.GetCorrectiveActionResponseByID(this.ID);
            }
            else
            {
                bizObject = new Business.Entities.ActionResponse();
                bizObject.CreatedBy = this.CreatedBy;
                bizObject.CreatedDate = DateTime.Now;
                bizObject.IsDeleted = false;
                //bizObject.IsReleased = false;
            }

            bizObject.UpdatedBy = this.UpdatedBy;
            bizObject.UpdatedDate = DateTime.Now;

            //if (this.HseqApprovalDate.HasValue)
            //    bizObject.HseqApprovalDate = this.HseqApprovalDate.Value;

            bizObject.HseqApprovalStatus = this.HseqApprovalStatus;
            bizObject.HseqComments = this.HseqComments;
            bizObject.IsDeleted = false;

            //if (this.OdApprovalDate.HasValue)
            //    bizObject.OdApprovalDate = this.OdApprovalDate.Value;

            bizObject.OdApprovalStatus = this.OdApprovalStatus;
            bizObject.OdComments = this.OdComments;
            bizObject.Response = this.Response;
            bizObject.ResponseIndex = this.ResponseIndex;
            bizObject.ResponseDate = DateTime.Now;
            bizObject.ResponseBy = this.ResponseBy;

            //if (this.ResponseDate.HasValue)
            //    bizObject.ResponseDate = this.ResponseDate.Value;

            //if (this.ReviewDate.HasValue)
            //    bizObject.ReviewDate = this.ReviewDate.Value;

            bizObject.ReviewerComments = this.ReviewerComments;
            bizObject.ReviewStatus = this.ReviewStatus;
            bizObject.HseqCommentsBy = this.HseqCommentsBy;
            bizObject.OdCommentsBy = this.OdCommentsBy;
            bizObject.ReviewCommentsBy = this.ReviewCommentsBy;
            bizObject.IsResponseReleased = this.IsResponseReleased;
            bizObject.IsReviewReleased = this.IsReviewReleased;
            bizObject.IsHseqApprovalReleased = this.IsHseqApprovalReleased;
            bizObject.IsOdApprovalReleased = this.IsOdApprovalReleased;

            if (this.Status.Trim() != string.Empty)
            bizObject.Status = (ActionResponseStatus)Enum.Parse(typeof(ActionResponseStatus), this.Status);

            return bizObject;
        }
    }

    [DataContract(Namespace = "Landscape")]
    public struct SearchActionResult
    {
        [DataMember]
        public Guid ID
        { get; set; }

        [DataMember]
        public string Code
        { get; set; }

        [DataMember]
        public string Title
        { get; set; }

        [DataMember]
        public string ActType
        { get; set; }

        [DataMember]
        public string Source
        { get; set; }

        [DataMember]
        public string CreatedBy
        { get; set; }

        [DataMember]
        public string CreatedDate
        { get; set; }

        [DataMember]
        public string AssignedTo
        { get; set; }

        [DataMember]
        public string ExpectedDate
        { get; set; }

        [DataMember]
        public string FinalizeDate
        { get; set; }

        [DataMember]
        public string Priority
        { get; set; }

        [DataMember]
        public string ClosedDate
        { get; set; }

        [DataMember]
        public string Status
        { get; set; }
    }
}
